package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.BusinessOutMapper;
import com.ant.storage.pojo.BusinessOut;
import com.ant.storage.pojo.BusinessOutCommodity;
import com.ant.storage.service.IBusinessOutService;
import com.ant.storage.service.ICityService;
import com.ant.storage.service.IInventoryService;
import com.ant.storage.utils.NullInspectUtil;
import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BusinessOutServiceImpl extends ServiceImpl<BusinessOutMapper,BusinessOut> implements IBusinessOutService{

    @Autowired(required = false)
    BusinessOutMapper businessOutMapper;

    @Autowired
    IInventoryService inventoryService;

    @Autowired
    ICityService cityService;

    @Override
    public ResultVo addBusinessOut(BusinessOut businessOut) {
        businessOut.setBusiness_out_type(1);
        if(this.save(businessOut)){
            for(BusinessOutCommodity businessOutCommodity:businessOut.getBusinessOutCommoditiesList()){
                businessOutMapper.addBusinessOutCommodity(businessOut.getBusiness_out_id(),businessOutCommodity.getCommodity_id(),businessOutCommodity.getApply_number());
            }
        }
        return ResultVo.success("ok");
    }

    @Override
    public ResultPage queryBusinessOut(PageRecv<BusinessOut> pageRecv) {
        Map map = new HashMap<>();
        if (!(pageRecv.getData()==null)){
            map = NullInspectUtil.ClassChangeMapNotNull(pageRecv.getData());
        }
        Integer page = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer limit = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<BusinessOut> allocation = businessOutMapper.queryAllocation(page,limit,map);
        for (BusinessOut businessOut:allocation) {
            String city = cityService.idQuery(businessOut.getCity_id());
            String dept = cityService.idQuery(businessOut.getDepartment_id());
            businessOut.getCity().setName(city);
            businessOut.getDepartment().setName(dept);
        }
        Integer count = businessOutMapper.countBusinessOut(map);
        return ResultPage.success("查询成功",allocation,count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo businessOutEnterEdit(BusinessOut businessOut, int i) {
        for(BusinessOutCommodity businessOutCommodity:businessOut.getBusinessOutCommoditiesList()){
            if(businessOutCommodity.getBusiness_out_number() == null){
                continue;
            }
            if (0 >= businessOutCommodity.getBusiness_out_number()||businessOutCommodity.getBusiness_out_number()>businessOutCommodity.getApply_number()){
                throw new AntException(ResultCode.ALLOCATION_OUT_NUMBER);
            }
            if (businessOutCommodity.getWarehouse().getId() == null){
                throw new AntException(ResultCode.WAREHOUSE_NULL);
            }
            if(inventoryService.commodityInventoryJudge(businessOutCommodity.getCommodity().getCommodityId(),businessOutCommodity.getWarehouse().getId(),businessOutCommodity.getBusiness_out_number())){
                throw new AntException(20001,businessOutCommodity.getCommodity().getCommodityName()+"库存不足");
            }
            businessOutMapper.updateBusinessOutCommodity(businessOut.getBusiness_out_id(),businessOutCommodity.getCommodity().getCommodityId(),businessOutCommodity.getWarehouse().getId(),businessOutCommodity.getBusiness_out_number());
        }
        if(this.getById(businessOut.getBusiness_out_id()).getOut_warehouse_time() == null){
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            businessOut.setOut_warehouse_time(simpleDateFormat.format(date));
        }
        businessOut.setBusiness_out_type(i);
        UpdateWrapper<BusinessOut>updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("business_out_id",businessOut.getBusiness_out_id());
        this.update(businessOut,updateWrapper);
        return ResultVo.success("ok");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stockSubtract(BusinessOut businessOut) {
        for(int a =0;a<businessOut.getBusinessOutCommoditiesList().size();a++){
            inventoryService.stockSubtract(businessOut.getBusinessOutCommoditiesList().get(a).getWarehouse().getId(),businessOut.getBusinessOutCommoditiesList().get(a).getCommodity().getCommodityId(),businessOut.getBusinessOutCommoditiesList().get(a).getBusiness_out_number());
        }
    }
}
